package org.db2.project.airline.dao.impl;

import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.dao.FlightClassPriceDAO;
import org.db2.project.airline.model.FlightClassPrice;
import org.db2.project.airline.model.FlightClassType;
import org.db2.project.airline.model.FlightPlan;
import org.hibernate.HibernateException;

/**
 *
 */
public class HibernateFlightClassPriceDAO extends HibernateGenericDAO<FlightClassPrice> implements FlightClassPriceDAO {

    public HibernateFlightClassPriceDAO() {
        // empty constructor
    }

    @Override
    public FlightClassPrice find(FlightPlan flightPlan, FlightClassType flightClassType) throws DAOException {

        try {
            return (FlightClassPrice) getSession().createQuery(
                    "select flightClassPrice from FlightClassPrice flightClassPrice" +
                            " where flightClassPrice.flightPlan.id = :flightPlanId and" +
                            " flightClassPrice.flightClass.id = :flightClassTypeId")
                    .setParameter("flightPlanId", flightPlan.getId())
                    .setParameter("flightClassTypeId", flightClassType.getId())
                    .uniqueResult();
        } catch (HibernateException e) {
            throw new DAOException("Error occurred when retrieving entity from persistence", e);
        }
    }
}
